package com.delong.dao;

import java.io.IOException;
import java.io.InputStream;

import javax.sql.DataSource;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class DaoHelper {

	private static SqlSessionFactory sqlSessionFactory;

	/*
	 * 创建sqlsessionfactory
	 */
	public static SqlSessionFactory getSqlSessionFactory() throws IOException {
		if (sqlSessionFactory == null) {
			// DataSource dataSource = getDataSource();
			// TransactionFactory transactionFactory = new
			// JdbcTransactionFactory();
			// Environment environment = new Environment("development",
			// transactionFactory, dataSource);
			// Configuration configuration = new Configuration(environment);
			// configuration.addMapper(userDao.class);
			// sqlSessionFactory = new
			// SqlSessionFactoryBuilder().build(configuration);
			String resource = "mybatis-config.xml";
//			File directory = new File("abc"); 
//			System.out.println(directory.getCanonicalPath());
			InputStream inputStream = Resources.getResourceAsStream(resource);
			
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		}
		return sqlSessionFactory;
	}

	public static DataSource getDataSource() {
		MysqlDataSource mysqlDS = null;
		mysqlDS = new MysqlDataSource();
		mysqlDS.setURL("jdbc:mysql://101.200.179.109/onlineMarket");
		mysqlDS.setUser("liyang");
		mysqlDS.setPassword("liyang123456");
		return mysqlDS;

	}

}
